package com.shuoyue.ycgk.ui.recruitment.job;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.JobInfo;
import com.shuoyue.ycgk.entity.RecruitmentCommentCourse;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.ui.recruitment.FocusChangeContract;
import com.shuoyue.ycgk.ui.recruitment.RecommentCourseAdapter;
import com.shuoyue.ycgk.ui.recruitment.newsinfo.NoticeAdapter;
import com.shuoyue.ycgk.ui.recruitment.newsinfo.RecurimentInfoActivity;
import com.shuoyue.ycgk.utils.ShareUtil;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JobDetailActivity extends BaseMvpActivity<JobInfoContract.Presenter>
        implements FocusChangeContract.View, JobInfoContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.postName)
    TextView postName;
    @BindView(R.id.postUnit)
    TextView postUnit;
    @BindView(R.id.postAreaName)
    TextView postAreaName;
    @BindView(R.id.personNum)
    TextView personNum;
    @BindView(R.id.status)
    TextView status;
    @BindView(R.id.postName2)
    TextView postName2;
    @BindView(R.id.examType)
    TextView examType;
    @BindView(R.id.postType)
    TextView postType;
    @BindView(R.id.workerUnit)
    TextView workerUnit;
    @BindView(R.id.upUnit)
    TextView upUnit;
    @BindView(R.id.major)
    TextView major;
    @BindView(R.id.majorMatch)
    TextView majorMatch;
    @BindView(R.id.education)
    TextView education;
    @BindView(R.id.educationMatch)
    TextView educationMatch;
    @BindView(R.id.ageRequirement)
    TextView ageRequirement;
    @BindView(R.id.ageRequirementMatch)
    TextView ageRequirementMatch;
    @BindView(R.id.hot1)
    TextView hot1;
    @BindView(R.id.hot2)
    TextView hot2;
    @BindView(R.id.hot3)
    TextView hot3;
    @BindView(R.id.hot4)
    TextView hot4;
    @BindView(R.id.recycleViewCourse)
    RecyclerView recycleViewCourse;
    @BindView(R.id.collection)
    ImageView collection;

    @Override
    public void setRecurimentInfo(JobInfo detail) {
        this.detail = detail;
        postName.setText(detail.getPostName());
        postAreaName.setText(detail.getAreaName()
                + "."
                + detail.getModuleName()
        );
        personNum.setText("~招聘"
                + detail.getPersonNum()
                + "人");
        postName2.setText(detail.getPostName());
        examType.setText(detail.getModuleName());
        workerUnit.setText(detail.getPostUnit());
        postUnit.setText(detail.getPostUnit());

        upUnit.setText(detail.getUpUnit());
        major.setText(detail.getMajor());
        education.setText(detail.getEducation());
        ageRequirement.setText(detail.getAgeRequirement());

        if (TextUtils.isEmpty(detail.getIsMatchAge())) {
            ageRequirementMatch.setText("?");
            ageRequirementMatch.setTextColor(Color.parseColor("#999999"));
        } else {
            ageRequirementMatch.setText(detail.getIsMatchAge().equals("1") ? "√" : "×");
            ageRequirementMatch.setTextColor(Color.parseColor(detail.getIsMatchAge().equals("1")
                    ? "#33E175" : "#FF7151"));
        }
        if (TextUtils.isEmpty(detail.getIsMatchMajor())) {
            majorMatch.setText("?");
            majorMatch.setTextColor(Color.parseColor("#999999"));
        } else {
            majorMatch.setText(detail.getIsMatchMajor().equals("1") ? "√" : "×");
            majorMatch.setTextColor(Color.parseColor(detail.getIsMatchMajor().equals("1")
                    ? "#33E175" : "#FF7151"));

        }
        if (TextUtils.isEmpty(detail.getIsMatchEducation())) {
            educationMatch.setText("?");
            educationMatch.setTextColor(Color.parseColor("#999999"));
        } else {
            educationMatch.setText(detail.getIsMatchEducation().equals("1") ? "√" : "×");
            educationMatch.setTextColor(Color.parseColor(detail.getIsMatchEducation().equals("1")
                    ? "#33E175" : "#FF7151"));
        }
        postType.setText(detail.getPostNature());
        collection.setImageDrawable(ContextCompat.getDrawable(mContext,
                detail.getIsCollect() == 1 ? R.mipmap.collection_y :
                        R.mipmap.collection_n));
        setHot(detail.getJobPopularity());
        if (detail.getRecommendProductDTOS() != null) {
            adapter.setNewData(detail.getRecommendProductDTOS());
        }
        if (detail.getStatus() == 1) {
            status.setText("即将报名");
            status.setBackgroundResource(R.mipmap.job_status_blue);
        } else if (detail.getStatus() == 0) {
            status.setText("正在报名");
            status.setBackgroundResource(R.mipmap.job_status_orange);
        } else {
            status.setText("报名结束");
            status.setBackgroundResource(R.mipmap.job_status_gray);
        }
    }

    int id;
    FocusChangeContract.Presenter focusPresenter;
    JobInfo detail;
    RecommentCourseAdapter adapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_job_detail;
    }

    @Override
    protected void initView() {
        pageTitle.setText("详情");

        recycleViewCourse.setLayoutManager(new LinearLayoutManager(mContext));
        recycleViewCourse.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));

        adapter = new RecommentCourseAdapter(null);
        recycleViewCourse.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            RecruitmentCommentCourse commentCourse = (RecruitmentCommentCourse) adapter.getItem(position);
            startActivity(new Intent(mContext, CourseInfoActivity.class)
                    .putExtra("id", commentCourse.getProductId()));
        });
    }

    @Override
    public void initData() {
        super.initData();
        id = getIntent().getIntExtra("id", -1);
        mPresenter = new JobInfoContract.Presenter();
        mPresenter.attachView(this);
        focusPresenter = new FocusChangeContract.Presenter();
        focusPresenter.attachView(this);
        mPresenter.getJobInfo(id);

    }

    @OnClick({R.id.share, R.id.collection, R.id.back, R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn:
                if (detail.getExamId() != 0) {
                    startActivity(new Intent(mContext, RecurimentInfoActivity.class)
                            .putExtra("id", detail.getExamId()));
                }
                break;
            case R.id.share:
                if (detail != null)
                    ShareUtil.shareUrl(this, ShareUtil.JOB_URL + id,
                            detail.getPostName(),
                            "",
                            null);
                break;
            case R.id.collection:
                if (detail != null)
                    focusPresenter.collectionNotice(detail.getId(), 2, detail.getIsCollect() == 1);
                break;
        }
    }

    @Override
    public void success(boolean isFocuse) {
        collection.setImageDrawable(ContextCompat.getDrawable(mContext,
                isFocuse ? R.mipmap.collection_y :
                        R.mipmap.collection_n));
        detail.setIsCollect(isFocuse ? 1 : 0);
    }

    @Override
    public void enableCollectButton() {
        collection.setEnabled(true);
    }

    @Override
    public void disAbleCollectButton() {
        collection.setEnabled(false);
    }

    private void setHot(int hotlvl) {
        hot1.setBackgroundColor(Color.parseColor(hotlvl > 0 ? "#FFAE9D" : "#C2C5CC"));
        hot2.setBackgroundColor(Color.parseColor(hotlvl > 1 ? "#FF9681" : "#C2C5CC"));
        hot3.setBackgroundColor(Color.parseColor(hotlvl > 2 ? "#FF765D" : "#C2C5CC"));
        hot4.setBackgroundColor(Color.parseColor(hotlvl > 3 ? "#FF5536" : "#C2C5CC"));

    }

}
